﻿using B.S.BasicData.Api.Read.Applications.Commands.Accounts;
using B.S.BasicData.Api.Read.Dtos;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastcuture.Commons;
using B.S.BasicData.Infrastcuture.Interfaces;
using MediatR;

namespace B.S.BasicData.Api.Read.Applications.CommandHandlers.Accounts
{

    /// <summary>
    /// 查询 参数
    /// </summary>
    public class GetQueryCommandHandler : IRequestHandler<GetUserQueryCommand, ApiResult<ApiPaging<ShowDto>>>
    {
        private readonly IBaseRepository<UserInfo> userbaseRepository;
        private readonly IBaseRepository<Dept> deptbaseRepository;
        private readonly IBaseRepository<Role> rolebaseRepository;

        public GetQueryCommandHandler(IBaseRepository<UserInfo> userbaseRepository, IBaseRepository<Dept> deptbaseRepository, IBaseRepository<Role> rolebaseRepository)
        {
            this.userbaseRepository = userbaseRepository;
            this.deptbaseRepository = deptbaseRepository;
            this.rolebaseRepository = rolebaseRepository;
        }


        public Task<ApiResult<ApiPaging<ShowDto>>> Handle(GetUserQueryCommand request, CancellationToken cancellationToken)
        {
           ApiResult<ApiPaging<ShowDto>> result=new ApiResult<ApiPaging<ShowDto>>();
            result.Code = ResultCode.SearchSuccess;
            result.Message = ResultCode.SearchSuccess.Description();
            var list = from a in userbaseRepository.GetAll()
                       join b in deptbaseRepository.GetAll() on a.DpetId equals b.DpetId
                       join c in rolebaseRepository.GetAll() on a.RoleId equals c.RoleId
                       where a.IsDelete!=1
                       select new ShowDto
                       {
                           UserId = a.UserId,
                           UserName = a.UserName,
                           NickName = a.NickName,
                           Phone = a.Phone,
                           Email = a.Email,
                           CreateTime = a.CreateTime,
                           RoleId = c.RoleId,
                           RoleName = c.RoleName,
                           DpetName = b.DpetName,
                           DpetId = b.DpetId,
                           State = a.State,  
                       };
            if(!string.IsNullOrEmpty(request.NickName))
            {
                list = list.Where(x => x.NickName.Equals(request.NickName));
            }
            if(request.RoleId>0)
            {
                list = list.Where(x => x.RoleId.Equals(request.RoleId));
            }
            if(request.DpetId>0)
            {
                list = list.Where(x=>x.DpetId.Equals(request.DpetId));

            }

            int tocom = list.Count();
            int topage = (int)Math.Ceiling(1.0 * tocom / request.PageSize);
            list = list.OrderByDescending(x => x.CreateTime).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            ApiPaging<ShowDto> page=new ApiPaging<ShowDto>();
            page.TotoCom=tocom; 
            page.TotoPage=topage;
            page.Data = list.ToList();
            result.Data = page;
            return Task.FromResult(result);
            

        }
    }
}
